import { getToken } from '@/utils/store'
import Article from '@/views/Article.vue'
import Collect from '@/views/Collect.vue'
import Detail from '@/views/Detail.vue'
import Layout from '@/views/Layout.vue'
import Like from '@/views/Like.vue'
import Login from '@/views/Login.vue'
import Register from '@/views/Register.vue'
import User from '@/views/User.vue'
import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const router = new VueRouter({
  routes: [
    {
      path: '/',
      component: Layout,
      children: [
        { path: '/', redirect: '/article' },
        { path: 'article', component: Article },
        { path: 'like', component: Like },
        { path: 'collect', component: Collect },
        { path: 'user', component: User }
      ]
    },
    { name: 'login', path: '/login', component: Login },
    { path: '/detail/:id', component: Detail },
    { path: '/register', component: Register }
  ]
})
const list = ['/login', '/register']
// 前置守卫
router.beforeEach((to, from, next) => {
  const token = getToken()
  if (token) {
    next()
  } else {
    if (list.includes(to.path)) {
      next()
    } else {
      next('/login')
    }
  }
})
export default router
